/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package beans;

import java.util.*;
import javax.ejb.*;
import entities.Event;
import entities.EventList;
import entities.EventType;
import java.util.ArrayList;

/**
 *
 * @author pwitek
 */
@Singleton
public class EventBean implements EventBeanLocal {
    @EJB
    private EventDAOLocal eventDAO;

    @EJB
    private EventTypeBeanLocal eventTypeBean;

    public String getEvents(double lat_lo, double lng_lo, double lat_hi, double lng_hi, int year, int day) {
        List<Event> events = eventDAO.getAllEvents();
        EventList visible = new EventList();
        for (Event event : events)
            if (lat_lo <= event.getLat() && event.getLat() <= lat_hi && lng_lo <= event.getLng() && event.getLng() <= lng_hi && event.getYear() == year)
                visible.add(event);
        return visible.toJSON();
    }

    public void insertEvent(double lat, double lng, int year, int day) {
        Event event = new Event(lat, lng, year, day);
        event.setType(eventTypeBean.getDefault());
        eventDAO.insertEvent(event);
    }

    public void updateEvent(Long id, String title, String description, String type, Double lat, Double lng, Integer year, Integer day) {
        Event event = new Event();
        event.setId(id);
        event.setTitle(title);
        event.setDescription(description);
        event.setType(eventTypeBean.getByName(type));
        event.setLat(lat);
        event.setLng(lng);
        event.setYear(year);
        event.setDay(day);
        eventDAO.updateEvent(event);
    }

    public void deleteEvent(long id) {
        eventDAO.deleteEvent(id);
    }
}
